package com.ddwl.wallet.controller;

import com.ddwl.common.annotation.Auth;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.wallet.IssueCouponBo;
import com.ddwl.schema.bo.wallet.UserCouponBo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.wallet.UserCouponVo;
import com.ddwl.wallet.service.UserCouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Optional;

/**
 * @Author hemiaojun
 * @Date 2021/9/7 17:59
 */
@Api(tags = "用户优惠卷")
@RequestMapping(value = "/coupon/user")
@RestController
@RequiredArgsConstructor
public class UserCouponController extends BaseController {

    private final UserCouponService userCouponService;

    @PostMapping("/list")
    @ApiOperation("用户优惠券")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE})
    public RestVo<List<UserCouponVo>> user(@Validated @RequestBody PageBo<UserCouponBo> pageBo) {
        UserCouponBo param = Optional.ofNullable(pageBo.getParam()).orElse(new UserCouponBo());
        param.setUserId(getUserId());
        pageBo.setParam(param);
        return RestVo.ok(userCouponService.user(pageBo));
    }

    @PostMapping("/take")
    @ApiOperation("领取优惠券")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE})
    public RestVo takeCoupon(@Valid @RequestBody UserCouponBo bo) {
        bo.setUserId(getUserId());
        userCouponService.takeCoupon(bo);
        return RestVo.ok();
    }


    @PostMapping("/issue")
    @ApiOperation("发放优惠券")
    @Auth(product = {BaseEnum.ProductEnum.API, BaseEnum.ProductEnum.SHOP_CONSOLE, BaseEnum.ProductEnum.CONSOLE})
    public RestVo issueCoupon(@Valid @RequestBody List<IssueCouponBo> list) {
        userCouponService.issueCoupon(list);
        return RestVo.ok();
    }

    @PostMapping("/check")
    @ApiOperation("核销优惠券")
    @Auth(product = BaseEnum.ProductEnum.API)
    public RestVo checkCoupon(@RequestParam String userCouponId,@RequestParam Boolean use) {
        userCouponService.checkCoupon(userCouponId,use);
        return RestVo.ok();
    }

    @PostMapping("/item")
    @ApiOperation("订单可以使用的券")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE})
    public RestVo<List<UserCouponVo>> item(@ApiParam("状态 0 未使用，1 已使用") @RequestBody List<String> itemIds) {
        return RestVo.ok(userCouponService.item(getUserId(), itemIds));
    }

    @PostMapping("/info")
    @ApiOperation("查看用户优惠券")
    @Auth(product = BaseEnum.ProductEnum.API)
    public RestVo<List<UserCouponVo>> info(@RequestBody List<String> userCouponIds) {
        return RestVo.ok(userCouponService.info(userCouponIds));
    }

}
